package com.qxgl.qxglt.dao;

import java.util.List;

import javax.transaction.Transactional;

import com.qxgl.qxglt.model.Roles;
import com.qxgl.qxglt.model.Users;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface IUsersDao extends JpaRepository<Users, String>, JpaSpecificationExecutor<Users> {



     // 登录
  Users findByLoginNameAndPassword(String loginName, String password);

  /*
   * @Transactional
   * 
   * @Modifying
   * 
   * @Query("delete from Users u where u.id=:id") int delUserById(String
   * id);//根据id删除用户
   */
  @Transactional
  @Modifying
  @Query("update Users u set u.password= 'ysd123' where u.id=:id")
  int pwdRest(String id);

  @Transactional
  @Modifying
  @Query("update Users u set u.isLockout = '解锁' where u.id = :id")
  int Lockout(String id);

  @Transactional
  @Modifying
  @Query("update Users u set u.isLockout = '锁定' where u.id = :id")
  int noLockout(String id);

  @Transactional
  @Modifying
  @Query(value = "DELETE from role_user where users_id=:userId and role_id in (:roleIds) ", nativeQuery = true)
  int detachUserByrolesList(String userId, List<String> roleIds);// 根据id删除用户

  @Query(value = "select roles.id,roles.name from roles where roles.id in ( select role_user.role_id from role_user  where role_user.users_id =?1)", nativeQuery = true)
  //@Query(value = "SELECT u.id,u.login_name,s.`name` from users u INNER JOIN role_user r on u.id=r.users_id INNER JOIN roles s ON r.role_id=s.id where u.id=?1",nativeQuery = true)
  List<Roles> findRoleById( String id);// 查询指定用户的角色





}